package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsShipmentOrder;
import com.jy.wms.pojo.WmsSplit;
import com.jy.wms.service.WmsSplitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsSplitController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Tue Oct 24 15:29:46 CST 2017
 */
@RestController
@RequestMapping("/wmsSplit")
@Api(value = "/wmsSplit", description = "关于拆单操作")
public class  WmsSplitController extends BaseController<WmsSplitService,WmsSplit> {

    @ApiOperation(value = "拆单操作  拆单",response = JSONObject.class, notes = "拆单，'ssfyddid'所属发运订单ID")
    @PostMapping(value = "/split")
    public @ResponseBody
    JSONObject split(HttpSession session ,@RequestBody SetAndModifyModel setAndModifyModel) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            baseService.split(setAndModifyModel,userinfo.getUser().getName(), String.valueOf(userinfo.getUser().getOrgId().get(0)));
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"拆单成功",e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"拆单失败",e.getMessage(), "");
        }
        return json;
    }

    @ApiOperation(value = "拆单操作  取消拆单",response = JSONObject.class, notes = "取消拆单，'ssfyddid'所属发运订单ID")
    @PostMapping(value = "/cancel")
    public @ResponseBody
    JSONObject cancel(HttpSession session ,@RequestBody SetAndModifyModel setAndModifyModel) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            baseService.baseDeleteBySsfyddid(setAndModifyModel,userinfo.getUser().getName(), String.valueOf(userinfo.getUser().getOrgId().get(0)));
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"取消拆单成功",e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"取消拆单失败","未拆单订单不可取消拆单", "");
        }
        return json;
    }

    @ApiOperation(value = "拆单操作  查看拆单明细",response = JSONObject.class, notes = "查看拆单明细，'ssfyddid'所属发运订单ID")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject cancel(HttpSession session ,@RequestBody WmsSplit entity) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            Page<Map<String,Object>> list = baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "发运订单  显示所有退单状态为“已退单未退货” SO列表",response = JSONObject.class, notes = "显示所有退单状态为“已退单未退货” SO列表")
    @PostMapping(value = "/getSoList")
    public @ResponseBody
    JSONObject getSoList(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        JSONObject json = getJSONObject();
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = "";
        //用户信息
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        Page<WmsShipmentOrder> list =   baseService.getSoList(entity, String.valueOf(userinfo.getUser().getOrgId().get(0)), hzid);
        json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        return json;
    }

    @ApiOperation(value = "发运订单  显示SO关联的已退单未退货订单",response = JSONObject.class, notes = "显示SO关联的已退单未退货订单")
    @PostMapping(value = "/getSoGlList")
    public @ResponseBody
    JSONObject getSoGlList(HttpSession session ,@RequestBody SetAndModifyModel setAndModifyModel) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            List<WmsShipmentOrder> list = baseService.getSoGlList(setAndModifyModel.getId(),hz.get(0).getHzid(), String.valueOf(userinfo.getUser().getOrgId().get(0)));
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "发运订单  退单重报",response = JSONObject.class, notes = "退单重报")
    @PostMapping(value = "/setTdzt")
    public @ResponseBody
    JSONObject setTdzt(HttpSession session ,@RequestBody SetAndModifyModel setAndModifyModel) {
        JSONObject json = getJSONObject();
        try {
            baseService.setTdzt(setAndModifyModel);
        } catch (SucessException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"退单重报成功",e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"退单重报失败",e.getMessage(), "");
        } catch (Exception e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"退单重报失败","", "");
        }
        return json;
    }

    @ApiOperation(value = "发运订单  退单",response = JSONObject.class, notes = "退单")
    @PostMapping(value = "/setTd")
    public @ResponseBody
    JSONObject setTd(HttpSession session ,@RequestBody SetAndModifyModel setAndModifyModel) {
        JSONObject json = getJSONObject();
        try {
            baseService.setTd(setAndModifyModel);
        } catch (SucessException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"退单成功",e.getMessage(), "");
        } catch (Exception e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"退单失败","", "");
        }
        return json;
    }

}
